local skel = fk.CreateSkill {
  name = "efengqi__congjian",
}

Fk:loadTranslationTable{
  ["efengqi__congjian"] = "从谏",
  [":efengqi__congjian"] = "当你指定或成为锦囊牌的目标后，可以将一张手牌交给不为你的目标角色，然后摸一张牌，其可以令你再摸一张牌。",

  ["#efengqi__congjian-give"] = "从谏：你可将一张手牌交给不为你的目标角色，摸1张牌，且其可令你再摸1张牌",
  ["#efengqi__congjian-ask"] = "从谏：你可以令 %src 摸一张牌",

  ["$efengqi__congjian1"] = "听君谏言，去危亡，保宗祀",
  ["$efengqi__congjian2"] = "从谏良计，可得自保！",
}

local on_cost = function(self, event, target, player, data)
  local room = player.room
  local targets = table.simpleClone(data.use.tos)
  table.removeOne(targets, player)
  local tos, cards = room:askToChooseCardsAndPlayers(player, {
    min_card_num = 1, max_card_num = 1, min_num = 1, max_num = 1, targets = targets, pattern = ".|.|.|hand",
    skill_name = skel.name, prompt = "#efengqi__congjian-give",
  })
  if #tos > 0 and #cards == 1 then
    event:setCostData(self, {tos = tos, cards = cards})
    return true
  end
end

local on_use = function(self, event, target, player, data)
  local room = player.room
  local to = event:getCostData(self).tos[1]
  room:obtainCard(to, event:getCostData(self).cards[1], true, fk.ReasonGive, player, skel.name)
  if not player.dead then
    player:drawCards(1, skel.name)
    if not player.dead and not to.dead and room:askToSkillInvoke(to,
      { skill_name = skel.name, prompt = "#efengqi__congjian-ask:"..player.id }) then
      room:doIndicate(to, {player})
      player:drawCards(1, skel.name)
    end
  end
end

skel:addEffect(fk.TargetConfirmed, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.card.type == Card.TypeTrick and not player:isKongcheng() then
      return table.find(data.use.tos, function (to) return to ~= player end) ~= nil
    end
  end,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addEffect(fk.TargetSpecified, {
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if target == player and player:hasSkill(skel.name) and data.card.type == Card.TypeTrick and not player:isKongcheng() then
      if not data.firstTarget then return false end
      return table.find(data.use.tos, function (to) return to ~= player end) ~= nil
    end
  end,
  on_cost = on_cost,
  on_use = on_use,
})

skel:addAI({
  think_skill_invoke = function (self, ai, skill_name, prompt)
    local data = ai.room.logic:getCurrentEvent().data
    local from = data.who -- 是否让这个人摸牌？
    if from == nil then return false end
    local ret = ai:getBenefitOfEvents(function(logic)
      logic:drawCards(from, 1, self.skill.name)
    end)
    return ret > 0
  end,
})

return skel
